package com.gun.management.module.raffle.controller.admin.lotteryrule;

import com.gun.management.module.raffle.controller.admin.lottery.vo.LotteryRespVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;

import com.gun.management.framework.excel.core.util.ExcelUtils;

import com.gun.management.module.raffle.controller.admin.lotteryrule.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryrule.LotteryRuleDO;
import com.gun.management.module.raffle.service.lotteryrule.LotteryRuleService;

@Tag(name = "管理后台 - 抽奖规则")
@RestController
@RequestMapping("/raffle/lotteryrule")
@Validated
public class LotteryRuleController {

    @Resource
    private LotteryRuleService lotteryRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建抽奖规则")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:create')")
    public CommonResult<Long> createLotteryRule(@Valid @RequestBody LotteryRuleSaveReqVO createReqVO) {
        return success(lotteryRuleService.createLotteryRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新抽奖规则")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:update')")
    public CommonResult<Boolean> updateLotteryRule(@Valid @RequestBody LotteryRuleSaveReqVO updateReqVO) {
        lotteryRuleService.updateLotteryRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除抽奖规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:delete')")
    public CommonResult<Boolean> deleteLotteryRule(@RequestParam("id") Long id) {
        lotteryRuleService.deleteLotteryRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得抽奖规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:query')")
    public CommonResult<LotteryRuleDO> getLotteryRule(@RequestParam("id") Long id) {
        LotteryRuleDO lotteryRule = lotteryRuleService.getLotteryRule(id);
        return success(lotteryRule);
    }

    @GetMapping("/page")
    @Operation(summary = "获得抽奖规则分页")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:page')")
    public CommonResult<PageResult<LotteryRuleRespVO>> getLotteryRulePage(@Valid LotteryRulePageReqVO pageReqVO) {
        PageResult<LotteryRuleDO> pageResult = lotteryRuleService.getLotteryRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LotteryRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出抽奖规则 Excel")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:export')")
    public void exportLotteryRuleExcel(@Valid LotteryRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LotteryRuleDO> list = lotteryRuleService.getLotteryRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "抽奖规则.xls", "数据", LotteryRuleRespVO.class,
                        BeanUtils.toBean(list, LotteryRuleRespVO.class));
    }


    @RequestMapping("/list")
    @Operation(summary = "获取list")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:list')")
    public CommonResult<List<LotteryRuleDO>> selectLotteryRuleList(@RequestParam("lotteryId") long lotteryId) {
        return success(lotteryRuleService.selectLotteryRuleList(lotteryId));
    }


    @GetMapping("/getGoodsPageList")
    @Operation(summary = "根据产品id,名称，商品分类 分获取产品及其sku列表")
   // @PreAuthorize("@ss.hasPermission('raffle:lotteryrule:getGoodsPageList')")
    public CommonResult<PageResult<GoodsRespVO>> getGoodsPageList(@Valid GoodsPageReqVO pageReqVO) {
        return success( BeanUtils.toBean(lotteryRuleService.selectGoodsPageList(pageReqVO),GoodsRespVO.class));
    }

}